package com.zx.crowdfunding.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zx.crowdfunding.entity.Role;
import com.zx.crowdfunding.entity.RoleExample;
import com.zx.crowdfunding.entity.RoleExample.Criteria;
import com.zx.crowdfunding.mapper.RoleMapper;
import com.zx.crowdfunding.service.api.RoleService;

/**
 * 角色 - Service接口实现类
 * @author 郑雪
 * @date 2021-12-09
 */
@Service
public class RoleServiceImpl implements RoleService {
	
	/**
	 * 角色Mapper接口
	 */
	@Autowired
	private RoleMapper roleMapper;

	@Override
	public PageInfo<Role> getPageInfo(String keyword, Integer pageNum,
			Integer pageSize) {
		
		// 1.调用PageHelper的静态方法开启分页功能
		PageHelper.startPage(pageNum, pageSize);
		
		// 2.执行查询
		List<Role> list = roleMapper.selectByKeyword(keyword);
		
		// 3.封装到PageInfo对象中
		return new PageInfo<Role>(list);
	}

	@Override
	public void saveRole(Role role) {
		roleMapper.insert(role);
	}

	@Override
	public void removeRole(List<Integer> idList) {
		RoleExample example = new RoleExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdIn(idList);
		roleMapper.deleteByExample(example);
	}

	@Override
	public Role getRoleById(Integer id) {
		return roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public void editRole(Role role) {
		roleMapper.updateByPrimaryKeySelective(role);
	}

	@Override
	public List<Role> getAssignedRole(Integer adminId) {
		return roleMapper.selectAssignedRole(adminId);
	}

	@Override
	public List<Role> getUnAssignedRole(Integer adminId) {
		return roleMapper.selectUnAssignedRole(adminId);
	}

	@Override
	public void saveRoleAuthRelationship(Integer roleId,
			List<Integer> authIdList) {
		
		// 根据roleID删除旧的关联关系数据
		roleMapper.deleteRoleAuthRelationship(roleId);
		
		// 添加角色与权限的关联关系数据
		if(authIdList != null && authIdList.size()>0){
			roleMapper.insertRoleAuthRelationship(roleId, authIdList);
		}
	}

}
